package com.ruoyi.web.controller;

import java.util.List;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.SysUserWatchthevideo;
import com.ruoyi.system.service.ISysUserWatchthevideoService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 用户观看视频Controller
 *
 * @author ruoyi
 * @date 2022-07-22
 */
@Api("用户观看视频管理")
@Controller
@RequestMapping("/system/watchthevideo")
public class SysUserWatchthevideoController extends BaseController {
    private String prefix = "system/watchthevideo";

    @Autowired
    private ISysUserWatchthevideoService sysUserWatchthevideoService;

    @RequiresPermissions("system:watchthevideo:view")
    @GetMapping()
    public String watchthevideo() {
        return prefix + "/watchthevideo";
    }

    /**
     * 查询用户观看视频列表
     */
    @ApiOperation("查询用户观看视频列表")
    @RequiresPermissions("system:watchthevideo:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(SysUserWatchthevideo sysUserWatchthevideo) {
        startPage();
        List<SysUserWatchthevideo> list = sysUserWatchthevideoService.selectSysUserWatchthevideoList(sysUserWatchthevideo);
        return getDataTable(list);
    }

    /**
     * 导出用户观看视频列表
     */
    @RequiresPermissions("system:watchthevideo:export")
    @Log(title = "用户观看视频", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(SysUserWatchthevideo sysUserWatchthevideo) {
        List<SysUserWatchthevideo> list = sysUserWatchthevideoService.selectSysUserWatchthevideoList(sysUserWatchthevideo);
        ExcelUtil<SysUserWatchthevideo> util = new ExcelUtil<SysUserWatchthevideo>(SysUserWatchthevideo.class);
        return util.exportExcel(list, "用户观看视频数据");
    }

    /**
     * 新增用户观看视频
     */
    @GetMapping("/add")
    public String add() {
        return prefix + "/add";
    }

    /**
     * 新增保存用户观看视频
     */
    @ApiOperation("修改保存用户观看视频")
    @RequiresPermissions("system:watchthevideo:add")
    @Log(title = "用户观看视频", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(SysUserWatchthevideo sysUserWatchthevideo) {
        return toAjax(sysUserWatchthevideoService.insertSysUserWatchthevideo(sysUserWatchthevideo));
    }

    /**
     * 修改用户观看视频
     */
    @RequiresPermissions("system:watchthevideo:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap) {
        SysUserWatchthevideo sysUserWatchthevideo = sysUserWatchthevideoService.selectSysUserWatchthevideoById(id);
        mmap.put("sysUserWatchthevideo", sysUserWatchthevideo);
        return prefix + "/edit";
    }

    /**
     * 修改保存用户观看视频
     */
    @ApiOperation("修改保存用户观看视频")
    @RequiresPermissions("system:watchthevideo:edit")
    @Log(title = "用户观看视频", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(SysUserWatchthevideo sysUserWatchthevideo) {
        return toAjax(sysUserWatchthevideoService.updateSysUserWatchthevideo(sysUserWatchthevideo));
    }

    /**
     * 删除用户观看视频
     */
    @ApiOperation("删除用户观看视频")
    @RequiresPermissions("system:watchthevideo:remove")
    @Log(title = "用户观看视频", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(sysUserWatchthevideoService.deleteSysUserWatchthevideoByIds(ids));
    }

    /** 添加用户观看进度
     * @param sysUserWatchthevideo
     * @return
     */
    @PostMapping("/myadd")
    @ResponseBody
    public AjaxResult myadd(SysUserWatchthevideo sysUserWatchthevideo) {
        SysUserWatchthevideo temp = new SysUserWatchthevideo();
        temp.setUserId(sysUserWatchthevideo.getUserId());
        temp.setVideoId(sysUserWatchthevideo.getVideoId());
        List<SysUserWatchthevideo> lists = sysUserWatchthevideoService.selectSysUserWatchthevideoList(temp);
        if (lists.size() == 0) {
            return toAjax(sysUserWatchthevideoService.insertSysUserWatchthevideo(sysUserWatchthevideo));
        } else {
            SysUserWatchthevideo item = lists.get(0);
            if ("0".equals(item.getVideoCondition()) && item.getVideoProgress() < sysUserWatchthevideo.getVideoProgress()) {
                sysUserWatchthevideo.setId(item.getId());
                return toAjax(sysUserWatchthevideoService.updateSysUserWatchthevideo(sysUserWatchthevideo));
            } else {
                return success();
            }
        }
    }

}
